package com.taosdata.taosdemo.dao;

import com.taosdata.taosdemo.domain.SuperTableMeta;
import com.taosdata.taosdemo.utils.SqlSpeller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author PC
 */
@Slf4j
public class SuperTableMapperImpl implements SuperTableMapper {
    private JdbcTemplate jdbcTemplate;

    public SuperTableMapperImpl(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void createSuperTable(SuperTableMeta tableMetadata) {
        String sql = SqlSpeller.createSuperTable(tableMetadata);
        log.info("SQL >>> " + sql);
        jdbcTemplate.execute(sql);
    }

    @Override
    public void dropSuperTable(String database, String name) {
        String sql = "drop table if exists " + database + "." + name;
        log.info("SQL >>> " + sql);
        jdbcTemplate.execute(sql);
    }
}
